package com.hzit.pay.web.service.strategy;

import com.hzit.pay.web.config.PayWebConfig;
import com.hzit.pay.web.factory.PayStrategyFactory;
import com.hzit.pay.web.iClient.IAliapyFrontClient;
import com.hzit.pay.web.model.MchInfo;
import com.hzit.pay.web.model.PaySerial;
import com.hzit.pay.web.service.IMchInfoService;
import com.hzit.pay.web.service.IPayStrategyService;
import com.hzit.payweb.api.alipay.AliapyQrCodeReq;
import com.hzit.payweb.api.enums.PayErrorMsgEnum;
import com.hzit.payweb.api.enums.PayWayEnum;
import com.hzit.payweb.api.resp.PayFrontRespData;
import com.hzit.payweb.api.resp.RespVo;
import com.hzit.payweb.api.resp.data.PayRespData;
import com.hzit.payweb.api.utils.PayDigestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付宝二维码支付
 */
@Component
public class AliapyQrCodePayImpl implements IPayStrategyService, InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(AliapyQrCodePayImpl.class);

    @Autowired
    private IAliapyFrontClient aliapyFrontClient;

    @Autowired
    private PayWebConfig payWebConfig;

    @Autowired
    private IMchInfoService mchInfoService;


    @Override
    public RespVo<PayRespData> pay(PaySerial paySerial) {
        logger.info("支付宝扫码支付，请求参数：{}",paySerial);
        AliapyQrCodeReq aliapyQrCodeReq = new AliapyQrCodeReq();
        aliapyQrCodeReq.setOutTraderNo(paySerial.getPaySerialNo());
        aliapyQrCodeReq.setBody(paySerial.getOrderSubject());
        aliapyQrCodeReq.setSubject(paySerial.getOrderSubject());
        //元转分
        aliapyQrCodeReq.setTotalAmt(paySerial.getPayAmt().toString());
        aliapyQrCodeReq.setNotifyUrl(payWebConfig.getNotifyUrl());

        //签名验证TODO

        RespVo<PayFrontRespData> respVo = aliapyFrontClient.alipayQrCodePay(aliapyQrCodeReq);
        logger.info("流水号：{}：调用支付宝前置服务，接口返回参数：{}",paySerial.getPaySerialNo(),respVo);


        if (respVo.getCode() != 0){
            logger.info("订单id：{}调用前置服务失败：{}",paySerial.getPaySerialNo(),respVo.getMsg());
            return RespVo.returnFail(PayErrorMsgEnum.ERROR.getCode(),PayErrorMsgEnum.ERROR.getDesc());
        }

        PayFrontRespData respData =  respVo.getData();

        MchInfo mchInfo = mchInfoService.getMchInfoByMchId("1000");

        //生成签名
        try {
           String sign = PayDigestUtil.getSign(respData,mchInfo.getRespKey());
            logger.info("sign = " + sign);
            RespVo<PayRespData> resp = new RespVo<PayRespData>();
            PayRespData payRespData = new PayRespData();
            payRespData.setSign(sign);
            payRespData.setOrderId(respData.getOutTradeNo());
            payRespData.setPayUrl(respData.getPayUrl());
            logger.info("订单id：{}支付宝扫码支付处理完成返回参数：{}",paySerial.getPaySerialNo(),resp);

            return resp;
        } catch (IllegalAccessException e) {
            logger.error("IllegalAccessException",e);
            return RespVo.returnFail(PayErrorMsgEnum.SIGN_CREATE_ERROE.getCode(),PayErrorMsgEnum.SIGN_CREATE_ERROE.getDesc());
        }




    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("-------初始化AliapyQrCodePayImpl-------------");

        PayStrategyFactory.concurrentHashMap.put(PayWayEnum.ALIPAY_QRCODE_PAY.getCode(),this);

    }
}
